import getLocationAuth from '@/utils/getLocationAuth.js'
export default {
  data() {
    return {
      pageLoading: false,
      city: '',//城市名
      search: '',//搜索的值
      scrollTop: 0,//顶部距离
      cityList: '',//尘世列表
      hasList: false,
      indexList: [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
        'U', 'W', 'X', 'Y', 'Z','#'
      ],
      newArr: [],
      arr: [], //搜索城市值
      a: [],//过滤的值
      isShow: false,
      pinyin: '',//拼音
      province: '',
      locationSuccess: false, // 定位是否成功
    }
  },

  onShow() {
    // let city = uni.getStorageSync('city')
    // this.city = city
    this.location()
  },

  created() {
    this.getCityList()
  },
  onPageScroll(e) {
    this.scrollTop = e.scrollTop
  },
  methods: {
    onCity(e){
      let data = {
        city: e.name,
        province: e.province ||'',
        latitude: e.Lat ||'',
        longitude: e.Lng ||''
      }
      uni.setStorageSync('lat',data.latitude)
      uni.setStorageSync('lon',data.longitude)
      uni.setStorageSync('province',data.province)
      uni.setStorageSync('city',data.city)
      this.$store.dispatch('changeAddress', {
        city: data.city,
        province: data.province,
        latitude: data.latitude,
        longitude: data.longitude
      })
      this.$store.dispatch('changeLatitude', data.latitude)
      this.$store.dispatch('changeLongitude', data.longitude)
      this.isShow = false
      uni.navigateBack({
        delta: 1,
      })
    },
    onBlur(){
      this.isShow = false
    },
    onChange(value) {
      this.search = value
      if (value.trim()) {
        this.isShow = true
        this.getSearch()
      } else {
        this.a=''
        this.isShow = false
      }
    },
    getSearch() {
      //过滤城市
      if(this.search.length>0){
        this.a = this.arr.filter(v => {
          return JSON.stringify(v).indexOf(this.search.toUpperCase()) !== -1 //JSON.stringify()是必须的
        })
      }
    },

    //定位
    location() {
      //#ifdef APP-PLUS
		  uni.getLocation({
		    type: 'gcj02',
		    geocode: true,
		    success: res => {
		      this.province = res.address.province
		      this.city = res.address.city
          this.locationSuccess = true
		    },
        fail: err => {
          this.city = null
          this.locationSuccess = false
        }
		  })
      //#endif
    },

    async getCityList() {
      try {
        const {
          data
        } = await this.$api.jsonData.regionalCityGroupNew()
        this.cityList = data.list
        this.newArr = data.list
        const newArr = this.newArr
        this.$nextTick(() => {
          this.pageLoading = true
        })

        for (let i = 0; i < newArr.length; i++) {
          let data = newArr[i].data
          for (let j = 0; j < data.length; j++) {
            // 开始替换
            let name = data[j]
            // name.push(this.arr)
            this.arr.push(name)
            // console.log(name)
          }
        }
      } catch (e) {
        console.log(e)
      }
    },
    addrestype(e,flag) {
      console.log(e)
      const handleCity = () => {
        let data = {
				  city: e.name,
				  province: e.province ||'',
				  latitude: e.Lat ||'',
				  longitude: e.Lng ||''
        }
        uni.setStorageSync('lat',data.latitude)
        uni.setStorageSync('lon',data.longitude)
        uni.setStorageSync('province',data.province)
        uni.setStorageSync('city',data.city)
        this.$store.dispatch('changeAddress', {
				  city: data.city,
				  province: data.province,
				  latitude: data.latitude,
				  longitude: data.longitude
        })
        this.$store.dispatch('changeLatitude', data.latitude)
        this.$store.dispatch('changeLongitude', data.longitude)
        uni.navigateBack({
				  delta: 1,
        })
      }
      if (this.locationSuccess) {
        handleCity()
      } else {
        if(flag){
          handleCity()
        }else{
          // 定位失败，表示系统没打开定位服务权限, 跳转到系统权限
          this.goSetting()
        }
      }
    },

    // 跳转到系统设置
    goSetting() {
      //#ifdef APP-PLUS
      let system = uni.getSystemInfoSync() // 获取系统信息
      if (system.platform === 'android') { // 判断平台
        var main = plus.android.runtimeMainActivity()
        var Intent = plus.android.importClass('android.content.Intent')
        var Settings = plus.android.importClass('android.provider.Settings')
        var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
        main.startActivity(intent) // 打开系统设置GPS服务页面
      } else if (system.platform === 'ios') {
        var UIApplication = plus.ios.importClass('UIApplication')
        var application2 = UIApplication.sharedApplication()
        var NSURL2 = plus.ios.importClass('NSURL')
        var setting2 = NSURL2.URLWithString('app-settings:') // 打开应用权限
        application2.openURL(setting2)
        plus.ios.deleteObject(setting2)
        plus.ios.deleteObject(NSURL2)
        plus.ios.deleteObject(application2)
      }
      //#endif
    }
  }
}
